<!DOCTYPE HTML>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
    description("This tests that multiple speak utterances are handled in sequence.");

    if (window.internals)
        internals.enableMockSpeechSynthesizer(window);

    if (window.testRunner)
        testRunner.waitUntilDone();

    window.jsTestIsAsync = true;

    var count = 3;
    var i = 0;

    // Start a very short speaking job that will finish quickly.
    var u = new SpeechSynthesisUtterance(" ");
    u.onstart = function(event) {
        shouldBeTrue("event.elapsedTime >= 0");
        shouldBeTrue("event.elapsedTime < 1000");
        shouldBeTrue("speechSynthesis.speaking");
    };
    u.onend = function(event) {
        shouldBeGreaterThanOrEqual("count", "i");
        shouldBeTrue("event.elapsedTime > 0");
        shouldBeTrue("event.elapsedTime < 10000");
        shouldBeTrue("speechSynthesis.speaking");
        if (++i == count) {
            speechSynthesis.cancel();
            // Add a new utterance just after the cancel() to signal test completion.
            speechSynthesis.speak(finishingUtterance);
        }
    };

    var finishingUtterance = new SpeechSynthesisUtterance(" ");
    finishingUtterance.onend = finishJSTest;

    shouldBeFalse("speechSynthesis.speaking");
    // Issue more utterances than where we break off.
    for (var j = 0; j < count + 2; ++j)
        speechSynthesis.speak(u);
</script>
</body>
</html>
